package book.book.service;

import book.book.bean.Books;
import book.book.bean.Borrowinfo;
import book.book.dao.BooksDao;
import book.book.dao.BooksDaoImpl;
import book.book.dao.BorrowinfoDao;
import book.book.dao.BorrowinfoDaoImpl;

import java.util.List;

public class BorrowinfoServiceImpl implements BorrowinfoService{
    BorrowinfoDao borrowinfoDao = new BorrowinfoDaoImpl();
    BooksDao booksDao = new BooksDaoImpl();
    @Override
    public boolean addBorrowinfo(Borrowinfo borrowinfo) {

        String borrower = borrowinfo.getBorrower();
        List<Borrowinfo> lists = borrowinfoDao.selectBorrwInfoByUser(borrower);
        if(lists.size() == 3){
            System.out.println("当前已经借了三本书，不能再借了！");
            return false;
        }
        String bookId = borrowinfo.getBookid();
       Books books =  booksDao.selectBookByBookId(bookId);
       if (books.getRemain() == 0){
           System.out.println("这本书已经借完了！");
           return false;
       }

        int i = borrowinfoDao.insert(borrowinfo);
        if(i>0){
            System.out.println("借阅信息添加成功！");
            return true;
        }
        return false;
    }
    public int updateBookCount(String bookId){
        return borrowinfoDao.updateBookCount(bookId);
    }

    @Override
    public List<Borrowinfo> selectBinfoByUser(String borrower) {
        return borrowinfoDao.selectBinfoByUser(borrower);

    }

    @Override
    public boolean updateBookCountBack(String bookId, String borrower) {
        int i = borrowinfoDao.updateBookCountBack(bookId,borrower);
        if(i>0){
            System.out.println("还书成功！");
            return true;
        }
        return false;
    }


}
